package xyz.scootaloo.oj.offer;

import org.junit.Test;

/**
 * 位运算
 * @author flutterdash@qq.com
 * @since 2021/4/9 15:46
 */
public class P15 {

    public int hammingWeight(int n) {
        int zeroCount = 0;
        while (n != 0) {
            int next = (n >> 1) << 1;
            if (n != next) {
                zeroCount++;
            }
            n = n >>> 1;
        }
        return zeroCount;
    }

    @Test
    public void test0() {
        int n = 3; // 11 : 2
        System.out.println(hammingWeight(n));
        int n1 = 11; // 1011 : 3
        System.out.println(hammingWeight(n1));
        int n2 = 64; // 1000000 : 1
        System.out.println(hammingWeight(n2));
        int n3 = Integer.MAX_VALUE - 3;
        System.out.println(hammingWeight(n3));
    }

}
